package edu.rit.drh4818.file;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

import edu.rit.drh4818.volume.Colormap;
import edu.rit.drh4818.volume.Volume;

/**
 * Volume data loaded from a file.
 * 
 * The file should be:
 * 
 * An int value indicating the size of the volume.
 * 
 * followed by the data, stored as shorts.
 * 
 * 
 * @author Darren Headrick
 *
 */
public class FileData extends Volume
{
	/**
	 * File
	 */
	private File voldata;
	
	/**
	 * Create a new Volume.
	 * 
	 * @param dimension Size of volume.
	 * @param name Volume name.
	 * @param volData File containing data.
	 */
	public FileData(int dimension, String name, File voldata ) 
	{
		super(dimension, false, name);
		this.voldata = voldata;
	}
	
	/**
	 * If the colormap gets changed, still need to regenerate the texture.
	 */
	public void setColormap( Colormap cmap )
	{
		super.setColormap( cmap );
		generateTexture();
	}
	
	/**
	 * Since data should be loaded only once, dont trample over it during setup.
	 */
	public void setup()
	{
		try
		{
			DataInputStream file = new DataInputStream(new FileInputStream( voldata ) );
	
			int xDim = file.readInt();
			volData = new short[xDim][xDim][xDim];
			
			for( int x = 0; x < volData.length; x++ )
				for( int y = 0; y < volData[x].length; y++ )
					for( int z = 0; z < volData.length; z++ )
					{
						volData[x][y][z] = file.readShort();
					}
			
			file.close();
			generateTexture();
		}
		catch( Exception e )
		{
		}
	}
	
	/**
	 * Since data should be loaded only once, dont trample over it during teardown.
	 */
	public void teardown()
	{
	}
	
	/**
	 * Noting changes.
	 */
	public void update()
	{	
	}
	
	/**
	 * Load the volume from a file.
	 * @param voldat File to load data from.
	 * @return
	 */
	public static Volume loadFromFile(File voldat)
	{
		try
		{
			DataInputStream file = new DataInputStream(new FileInputStream( voldat ) );
			int xDim = file.readInt();
			return new FileData( xDim, "From file", voldat ) ;
		}
		catch( Exception e )
		{
			System.out.println( e );
		}
		return null;
	}
}
